\defmodule {OrnsteinUhlenbeckProcess}

This class represents an \emph{Ornstein-Uhlenbeck} process
 $\{X(t) : t \geq 0 \}$, sampled at times $0 = t_0 < t_1 < \cdots < t_d$.
This process obeys the stochastic differential equation
\begin{equation}
   dX(t) = \alpha(b - X(t)) dt + \sigma\, dB(t)
                                               \label{eq:ornstein}
\end{equation}
with initial condition $X(0)= x_0$, 
where $\alpha$, $b$ and $\sigma$ are positive constants,
and $\{B(t),\, t\ge 0\}$ is a standard Brownian motion
(with drift 0 and volatility 1).
This process is \emph{mean-reverting} in the sense that it always tends to
drift toward its general mean $b$.
The process is generated using the sequential technique \cite[p. 110]{fGLA04a}
\begin{equation}
   X(t_j) = e^{-\alpha(t_j - t_{j-1})} X(t_{j-1}) +
            b\left(1 - e^{-\alpha(t_j - t_{j-1})}\right) +
      \sigma \sqrt{\frac{1 - e^{-2\alpha(t_j - t_{j-1})}}{2\alpha}}\, Z_j
                                    \label{eq:ornstein-seq}
\end{equation}
where $Z_j \sim {N}(0,1)$. The time intervals $t_j - t_{j-1}$
can be arbitrarily large.

\bigskip\hrule\bigskip

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{code}
\begin{hide}
/*
 * Class:        OrnsteinUhlenbeckProcess
 * Description:  
 * Environment:  Java
 * Software:     SSJ 
 * Copyright (C) 2001  Pierre L'Ecuyer and Universite de Montreal
 * Organization: DIRO, Universite de Montreal
 * @author       
 * @since

 * SSJ is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License (GPL) as published by the
 * Free Software Foundation, either version 3 of the License, or
 * any later version.

 * SSJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * A copy of the GNU General Public License is available at
   <a href="http://www.gnu.org/licenses">GPL licence site</a>.
 */
\end{hide}
package umontreal.iro.lecuyer.stochprocess;\begin{hide}
import umontreal.iro.lecuyer.rng.*;
import umontreal.iro.lecuyer.probdist.*;
import umontreal.iro.lecuyer.randvar.*;

\end{hide}

public class OrnsteinUhlenbeckProcess extends StochasticProcess \begin{hide} {
    protected NormalGen    gen;
    protected double       alpha,
                           beta,
                           sigma;
    // Precomputed values 
    protected double[]     badt,
                           alphadt,
                           sigmasqrdt;
\end{hide}
\end{code}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection* {Constructors}
\begin{code}

   public OrnsteinUhlenbeckProcess (double x0, double alpha, double b,
                                    double sigma, RandomStream stream)\begin{hide} {
        this (x0, alpha, b, sigma, new NormalGen (stream));
    }\end{hide}
\end{code}
\begin{tabb} Constructs a new \texttt{OrnsteinUhlenbeckProcess} with parameters
 $\alpha =$ \texttt{alpha}, $b$, $\sigma =$ \texttt{sigma} and initial value
$X(t_{0}) =$ \texttt{x0}. The normal variates $Z_j$ will be 
generated by inversion using the stream \texttt{stream}.
\end{tabb}
\begin{code}

   public OrnsteinUhlenbeckProcess (double x0, double alpha, double b,
                                    double sigma, NormalGen gen) \begin{hide} {
      this.alpha = alpha;
      this.beta  = b;
      this.sigma = sigma;
      this.x0    = x0;
      this.gen   = gen;
   }\end{hide}
\end{code}
\begin{tabb} Here, the normal variate generator is specified directly
instead of specifying the stream.
The normal generator \texttt{gen} can use another method than inversion.
\end{tabb}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection* {Methods}
\begin{code}\begin{hide}

   public double nextObservation() {
        double xOld = path[observationIndex];
        double x = badt[observationIndex] + xOld * alphadt[observationIndex]
                   + sigmasqrdt[observationIndex] * gen.nextDouble();
        observationIndex++;
        path[observationIndex] = x;
        return x;
    }\end{hide}

   public double nextObservation (double nextTime) \begin{hide} {
        double previousTime = t[observationIndex];
        double xOld = path[observationIndex];
        observationIndex++;
        t[observationIndex] = nextTime;
        double dt = nextTime - previousTime;
        double tem = Math.exp(-alpha * dt);
        double tem1 = -Math.expm1(-alpha * dt);
        double x = tem*xOld + beta*tem1 + sigma *
            Math.sqrt(tem1*(1.0 + tem)/(2.0*alpha)) * gen.nextDouble();
        path[observationIndex] = x;
        return x;
    }\end{hide}
\end{code}
\begin{tabb} Generates and returns the next observation at time $t_{j+1} =$
 \texttt{nextTime}, using the previous observation time $t_{j}$ defined earlier 
(either by this method or by \texttt{setObservationTimes}), 
as well as the value of the previous observation $X(t_j)$. 
\emph{Warning}: This method will reset the observations time $t_{j+1}$
for this process to \texttt{nextTime}. The user must make sure that
the $t_{j+1}$ supplied is $\geq t_{j}$.
\end{tabb}
\begin{code}

   public double nextObservation (double x, double dt) \begin{hide} {
        double tem = Math.exp(-alpha * dt);
        double tem1 = -Math.expm1(-alpha * dt);
        x = tem*x + beta*tem1 + sigma *
            Math.sqrt(tem1*(1.0 + tem)/(2.0*alpha)) * gen.nextDouble();
        return x;
    }\end{hide}
\end{code}
\begin{tabb} Generates an observation of the process in \texttt{dt} time units,
assuming that the process has value $x$ at the current time.
Uses the process parameters specified in the constructor.
Note that this method does not affect the sample path of the process 
stored internally (if any).
\end{tabb}
\begin{hide}\begin{code}

   public double[] generatePath() {
        double x;
        double xOld = x0;
        for (int j = 0; j < d; j++) {
            x = badt[j] + xOld * alphadt[j] + sigmasqrdt[j] * gen.nextDouble();
            path[j + 1] = x;
            xOld = x;
        }
        observationIndex = d;
        return path;
    }

   public double[] generatePath (RandomStream stream) {
        gen.setStream (stream);
        return generatePath();
    }
\end{code}
\begin{tabb} Generates a sample path of the process at all observation times,
 which are provided in array \texttt{t}.
 Note that \texttt{t[0]} should be the observation time of \texttt{x0}, 
 the initial value of the process, and \texttt{t[]} should have at least $d+1$
 elements (see the \texttt{setObservationTimes} method).
\end{tabb}\end{hide}
\begin{code}

   public void setParams (double x0, double alpha, double b, double sigma) \begin{hide} { 
        this.alpha = alpha;
        this.beta  = b;
        this.sigma = sigma;
        this.x0    = x0;
        if (observationTimesSet) init(); // Otherwise not needed.
    }\end{hide}
\end{code}
\begin{tabb} 
Resets the parameters $X(t_{0}) =$ \texttt{x0}, $\alpha =$ \texttt{alpha},
 $b =$ \texttt{b} and $\sigma =$ \texttt{sigma} of the process. 
\emph{Warning}: This method will recompute some quantities stored internally, 
which may be slow if called too frequently.
\end{tabb}
\begin{code}

   public void setStream (RandomStream stream) \begin{hide} { gen.setStream (stream); }\end{hide}
\end{code}
\begin{tabb} 
Resets the random stream of the normal generator to \texttt{stream}.
\end{tabb}
\begin{code}

   public RandomStream getStream () \begin{hide} { return gen.getStream (); }\end{hide}
\end{code}
\begin{tabb} 
Returns the random stream of the normal generator.
\end{tabb}
\begin{code}

   public double getAlpha() \begin{hide} { return alpha; }\end{hide}
\end{code}
\begin{tabb} 
Returns the value of $\alpha$.
\end{tabb}
\begin{code}

   public double getB() \begin{hide} { return beta; }\end{hide}
\end{code}
\begin{tabb} 
Returns the value of $b$.
\end{tabb}
\begin{code}

   public double getSigma() \begin{hide} { return sigma; }\end{hide}
\end{code}
\begin{tabb} 
Returns the value of $\sigma$.
\end{tabb}
\begin{code}

   public NormalGen getGen() \begin{hide} { return gen; }\end{hide}
\end{code}
\begin{tabb} 
Returns the normal random variate generator used.
The \texttt{RandomStream} used for that generator can be changed via 
\texttt{getGen().setStream(stream)}, for example.
\end{tabb}
\begin{code} \begin{hide}

   protected void initArrays(int d) {
      double dt, tem, tem1;
      for (int j = 0; j < d; j++) {
         dt = t[j+1] - t[j];
         tem = Math.exp(-alpha * dt);
         tem1 = -Math.expm1(-alpha * dt);
         badt[j] = beta*tem1;
         alphadt[j] = tem;
         sigmasqrdt[j] = sigma * Math.sqrt (tem1*(1.0 + tem)/(2.0*alpha));
      }
   }

    // This is called by setObservationTimes to precompute constants
    // in order to speed up the path generation.
    protected void init() {
        super.init();
        badt = new double[d];
        alphadt = new double[d];
        sigmasqrdt = new double[d];
        initArrays(d);
     }\end{hide}
\end{code}
\begin{code}\begin{hide}
} \end{hide}
\end{code}
